package com.bq.gametrade.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bq.gametrade.HttpLink;
import com.bq.gametrade.R;
import com.bq.gametrade.adapter.InterconnectedDetailsListAdapter;
import com.bq.gametrade.base.BaseActivity;
import com.bq.gametrade.bean.Interconnected;
import com.bq.gametrade.bean.InterconnectedDetails;
import com.bq.gametrade.bean.UserInfo;
import com.bq.gametrade.bean.UserInfoDetails;
import com.bq.gametrade.http.VRHttp;
import com.bq.gametrade.http.VRHttpListener;
import com.bq.gametrade.view.pullableview.PullToRefreshLayout;
import com.bq.gametrade.view.pullableview.PullableListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class InterconnectedDetailsListActivity extends BaseActivity implements View.OnClickListener {
    public static int pageIndex = 1;
    private Activity activity;
    private ImageView back;
    private TextView tvTitle;

    private PullToRefreshLayout refreshLayout;
    private PullableListView listView;

    private ArrayList<InterconnectedDetails> mList = new ArrayList<>();
    private InterconnectedDetailsListAdapter adapter;
    private LinearLayout layPrompt;
    private UserInfoDetails userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interconnecteddetails_list);
        if(application.isLogin()){
            userInfo = application.getUserInfo();
        }else {
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }
        activity = this;
        initView();
        initListView();
        getInfo();
    }

    private void initView() {
        back = serchViewById(R.id.image_back);
        back.setOnClickListener(this);
        tvTitle = serchViewById(R.id.title_name);
        tvTitle.setText("互联明细");
        refreshLayout = serchViewById(R.id.layout_refresh);
        listView = serchViewById(R.id.lv_transferDetails);
        layPrompt = serchViewById(R.id.lay_prompt);

    }

    private void initListView() {

        adapter = new InterconnectedDetailsListAdapter(InterconnectedDetailsListActivity.this, mList);
        listView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                listViewInfo(1, pullToRefreshLayout, 0);
                toastShow("刷新完成");
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                // 加载操作
                listViewInfo(1, pullToRefreshLayout, 1);
                toastShow("加载完成");
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back:
                finish();
                break;
        }
    }

    private void getInfo() {
        Map<String, String> params = new HashMap<>();
        params.put("token", userInfo.token);
        params.put("curPage", "1");
        showSpotsDialog();
        VRHttp.getInterconnectedList(activity, HttpLink.INTERINTERCONNECTED_DETAILS_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                closeSpotsDialog();
                if (response != null) {
                    Interconnected interconnected = (Interconnected) response;
                    if (interconnected.status.equals("0")) {
                        mList = interconnected.transfer_list;
                        if(mList.size()>0){
                            layPrompt.setVisibility(View.GONE);
                            adapter.setData(mList);
                            adapter.notifyDataSetChanged();
                        }else {
                            layPrompt.setVisibility(View.VISIBLE);
                        }

                        ++InterconnectedDetailsListActivity.pageIndex;
                    } else if (interconnected.status.equals("99")) {
                        toastShow("登录超时请重新登陆");
                        application.setUserInfo(null);
                        startActivity(new Intent(InterconnectedDetailsListActivity.this, LoginActivity.class));
                        finish();
                    }else if (interconnected.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(InterconnectedDetailsListActivity.this, LoginActivity.class));
                        finish();
                    }
                } else {
                    toastShow("获取数据失败");

                }
            }
            @Override
            public void onError(String error) {
                super.onError(error);
                closeSpotsDialog();
                toastShow("获取数据失败");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }

    private void listViewInfo(int pageIndex, final PullToRefreshLayout pullToRefreshLayout, final int type) {
        Map<String, String> params = new HashMap<>();
        params.put("token", userInfo.token);
        params.put("curPage", String.valueOf(pageIndex));
        VRHttp.getInterconnectedList(activity, HttpLink.INTERINTERCONNECTED_DETAILS_URL, params, new VRHttpListener() {
            @Override
            public void onSuccess(Object response, boolean isCache) {
                if (response != null) {

                    Interconnected interconnected = (Interconnected) response;
                    if (interconnected.status.equals("0")) {
                        mList = interconnected.transfer_list;
                        adapter.setData(mList);
                        adapter.notifyDataSetChanged();
                        ++InterconnectedDetailsListActivity.pageIndex;
                        if (pullToRefreshLayout != null) {
                            pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        }
                        if (type == 1) {
                            ++InterconnectedDetailsListActivity.pageIndex;
                        }
                    } else if (interconnected.status.equals("99")) {
                        toastShow("登录超时请重新登陆");
                        application.setUserInfo(null);
                        startActivity(new Intent(InterconnectedDetailsListActivity.this, LoginActivity.class));
                        finish();
                    }else if (interconnected.status.equals("98")) {
                        toastShow("您的账号已在其它设备登录，请重新登录修改您的密码");
                        application.setUserInfo(null);
                        startActivity(new Intent(InterconnectedDetailsListActivity.this, LoginActivity.class));
                        finish();
                    }
                } else {
                    toastShow("获取数据失败");
                    if (pullToRefreshLayout != null) {
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                    }
                }
            }

            @Override
            public void onError(String error) {
                super.onError(error);
                toastShow("获取数据失败");
                if (pullToRefreshLayout != null) {
                    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeSpotsDialog();
            }
        });
    }
}
